;(function($, window, document,undefined) {
        var Keyboard = function(boxEl,trEl,ele,callback){
        	this.$boxEl = boxEl;
            this.$element = ele;
            this.trEl = trEl;
            this.callback = callback;
        };
        Keyboard.prototype = {
            //初始化生成界面
            init:function(){
            	//将表格中的值赋值到input中
            	$('#'+this.$boxEl).find('input[jisuaninput]').val($(this.$element).text());
//            	$('#'+this.$boxEl).find('input[jisuaninput]')[0].select()
                this.my_clear();
                this.done();
                this.my_ok();
                this.my_backspace();
                this.my_next();
            },
            //确定
            my_ok:function(){
                var that = this;
                $('#'+that.$boxEl).find('div[button-key="my_ok"]').off('click');
                $('#'+that.$boxEl).find('div[button-key="my_ok"]').click(function(){
                	$(that.$element).html('<div class="layui-table-cell laytable-cell-1-0-5">'+$('#'+that.$boxEl).find('input[jisuaninput]').val()+'</div>');
//                	$(that.$element).text($('#'+that.$boxEl).find('input[jisuaninput]').val());
                	$('#'+that.$boxEl).find('input[jisuaninput]').val('0');
                    that.callback();
                });
            },
            //退格
            my_backspace:function(){
                var that = this;
                $('#'+that.$boxEl).find('div[button-key="my_backspace"]').off('click');
                $('#'+that.$boxEl).find('div[button-key="my_backspace"]').click(function(){
                    var input_val = $('#'+that.$boxEl).find('input[jisuaninput]').val();
                    input_val = input_val.substring(0, input_val.length - 1);
                    if(input_val == ''){
                    	input_val = '0';
                    }
                    $('#'+that.$boxEl).find('input[jisuaninput]').val( input_val );
//                    $(that.$element).text( input_val );
                });
            },
            //清除
            my_clear:function(){
                var that = this;
                $('#'+that.$boxEl).find('div[button-key="my_clear"]').off('click');
                $('#'+that.$boxEl).find('div[button-key="my_clear"]').click(function(){
                	$('#'+that.$boxEl).find('input[jisuaninput]').val('0');
                	$(that.$element).html('<div class="layui-table-cell laytable-cell-1-0-5">0</div>');
//                    $(that.$element).text('0');
                   that.callback();
                });
            },
            //赋值
            done: function() {
                var that = this;
                $('#'+that.$boxEl).find('div[num_key]').off('click');
                $('#'+that.$boxEl).find('div[num_key]').on('click',function(){
                   var num = $(this).attr('num_key');
                   var input_val = $('#'+that.$boxEl).find('input[jisuaninput]').val()=='0'?'':$('#'+that.$boxEl).find('input[jisuaninput]').val();
                   var val = input_val + num;
                   if($(that.$element).text() == input_val){
                	   val = num;
                   }
                   val = Number(val);
                   $('#'+that.$boxEl).find('input[jisuaninput]').val(val);
//                   $(that.$element).text(val);
//                   that.callback();
                });
            },
            //下一行
			my_next: function() {
				var that = this;
                $('#'+that.$boxEl).find('div[button-key="my_next"]').off('click');
                $('#'+that.$boxEl).find('div[button-key="my_next"]').click(function(){
//                	$(that.$element).text($('#'+that.$boxEl).find('input[jisuaninput]').val());
                	$(that.$element).html('<div class="layui-table-cell laytable-cell-1-0-5">'+$('#'+that.$boxEl).find('input[jisuaninput]').val()+'</div>');
//                	$('#'+that.$boxEl).find('input[jisuaninput]').val('0');
//                	console.log()
                    that.callback();
                    if(that.trEl['next']().length != 0){
                    	that.trEl['next']().click();
                    }else{
                    	that.trEl.parent().find('tr:eq(0)').click();
                    }
                });
			}
			/*	$(mycalculator).click(function(event) {
				        event.stopPropagation(); //阻止事件向上冒泡
				 });
				$(document).one("click",
			        function() { 
			            $(mycalculator).remove();
 						that.callback();
 						event.stopPropagation(); //阻止事件向上冒泡
		        });*/
				
 	 		

/*                $('span.num_key').on('click',function(){
                   var num = $(this).attr('the_val');
                   var input_val = $(that.$element).text()=='0'?'':$(that.$element).text();
                   var val = input_val + num;
                   
                   $(that.$element).text( val );
//                   that.callback();
                });*/
         

        };



        $.fn.mykeyboard = function(boxEl,trEl,callback) {
        		//直接执行
	        	var my = new Keyboard(boxEl,trEl,this,callback);
	            my.init();
	            //获取焦点执行
//                this.on('focus',function(){
//                    var my = new Keyboard(this, options);
//                    my.init();
//                });

        };
})(jQuery, window, document);